package io.renren.common.utils;

import io.renren.common.xss.SQLFilter;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 查询参数
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-14 23:15
 */
public class Query extends LinkedHashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    // 当前页码
    private int current;
    // 每页条数
    private int size;

    public Query(Map<String, Object> params) {
        this.putAll(params);

        // 分页参数
        this.current = Integer.parseInt(params.get("current").toString());
        this.size = Integer.parseInt(params.get("size").toString());
        this.put("offset", (current - 1) * size);
        this.put("page", current);
        this.put("limit", size);

        // 防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
        String sidx = params.get("sidx").toString();
        String order = params.get("order").toString();
        this.put("sidx", SQLFilter.sqlInject(sidx));
        this.put("order", SQLFilter.sqlInject(order));
    }

    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        this.current = current;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

}
